﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Formatters.Xml;
using DeMeng.Common.Config;
using DeMeng.Common.HttpContextUser;
using DeMeng.Models;
using DeMeng.Models.Models;
using DeMeng.Models.Models.UserInfo;
using DeMeng.Models.Swagger;
using DeMeng.Services.Domain;

namespace DeMeng.Api.Areas.Manager.Controllers
{
    [ApiExplorerSettings(GroupName = Grouping.V2)]
    [Route("Manager/System/[controller]")]
    [ApiController]
    [Authorize(Roles = "Boss,Manager")]
    public class UserInfoController : ControllerBase
    {

        IUserInfoServices _userInfoServices;
        private readonly IAspNetUser _aspNetUser;

        public UserInfoController(IUserInfoServices userInfoServices, IAspNetUser aspNetUser)
        {
            _userInfoServices = userInfoServices;
            _aspNetUser = aspNetUser;

        }

        /// <summary>
        /// 获取表单信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost(nameof(LoadForm))]
        public async Task<ResponseModel> LoadForm(GuidQueryModel query)
        {
            var model = await _userInfoServices.QueryById(query.Id);
            //model.RoleIdList = _roleServices.GetUserRoleList(model.ID).Result;
            if (model != null)
            {
                model.HeaderImage = string.IsNullOrWhiteSpace(WebConfig.WebSiteUrl) ? "" : WebConfig.WebSiteUrl + model.HeaderImage;
            }
            return ResponseModel.Success(model == null ? new UserInfoEntity() { ID = 0 } : model);
        }

        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="model">用户信息</param>
        /// <returns></returns>
        [HttpPost(nameof(Save))]
        public ResponseModel Save(UserInfoEntity model)
        {
            if (_userInfoServices.Save(model))
            {
                return ResponseModel.Success(model);
            }
            throw new ApiException("保存失败");

        }

        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost(nameof(FindList))]
        public async Task<ResponseModel> FindList(UserInfoListQueryModel query)
        {
            return ResponseModel.Success(await _userInfoServices.FindList(query));
        }


        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="query">ID</param>
        /// <returns></returns>
        [HttpPost(nameof(Delete))]
        public async Task<ResponseModel> Delete(GuidQueryModel query)
        {
            return ResponseModel.Success(await _userInfoServices.DeleteById(query.Id));
        }


        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost(nameof(ResetPassword))]
        public async Task<ResponseModel> ResetPassword(ResetPasswordQueryModel model)
        {
            return ResponseModel.Success(await _userInfoServices.ResetPassword(model));
        }

        
        
        /// <summary>
        /// 设置禁用
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost(nameof(SetDisable))]
        public async Task<ResponseModel> SetDisable(OperateQueryModel query)
        {
            return ResponseModel.Success(await _userInfoServices.SetDisable(query));
        }

    }
}